public class ParentViewModel
{
public string sITEM_NUMBER { get; set; }
public string rITEM_NUMBER { get; set; }
public string SOURCE_ID { get; set; }
public string RELATED_ID { get; set; }
public string STATE { get; set; }
public string pMAJOR_REV { get; set; }
public string sMAJOR_REV { get; set; }
public string pGENERATION { get; set; }
public string sGENERATION { get; set; }
}
接下來回到Controller的部分,首先先到全域的區域建立Model,得以在Controller中取得Model中資料表的資料
這邊的範例為延續昨天的LinqKit自訂查詢資料表where_CAD_STRUCTURE以及結合db.CAD兩個資料表,接著在select new剛剛建立的ParentViewModel,將兩個資料表結合並寫入ViewModel當中,若想了解如何利用LinqKit自訂查詢的朋友可以看 (https://ithelp.ithome.com.tw/articles/10225284) 的介紹
IEnumerable<ParentViewModel> parentModel =
(
from cad_str in where_CAD_STRUCTURE
join source_cad in db.CAD on cad_str.SOURCE_ID equals source_cad.ID
join related_cad in db.CAD on cad_str.RELATED_ID equals related_cad.ID
where source_cad.IS_CURRENT == "1"
orderby related_cad.ITEM_NUMBER, source_cad.ITEM_NUMBER
select new ParentViewModel
{
sITEM_NUMBER = source_cad.ITEM_NUMBER,
rITEM_NUMBER = related_cad.ITEM_NUMBER,
SOURCE_ID = cad_str.SOURCE_ID,
RELATED_ID = cad_str.RELATED_ID,
STATE = source_cad.STATE,
pMAJOR_REV = source_cad.MAJOR_REV,
sMAJOR_REV = related_cad.MAJOR_REV,
pGENERATION = source_cad.GENERATION.ToString(),
sGENERATION = related_cad.GENERATION.ToString()
}
);
return View(parentModel);
@model IEnumerable<TLTC_CADChange.Library.ParentViewModel>
<table class="table table-bordered table-striped">
<thead>
<tr style="background-color: #cdcdcd;">
<th>
共用鍵 @ViewBag.son 的 @ViewBag.level 父階圖號
</th>
<th>
子階圖號
</th>
<th>
狀態
</th>
<th>
版本
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.sITEM_NUMBER)
</td>
<td>
@Html.DisplayFor(modelItem => item.rITEM_NUMBER)
</td>
<td>
@Html.DisplayFor(modelItem => item.STATE)
</td>
<td>
@Html.DisplayFor(modelItem => item.pMAJOR_REV)
</td>
</tr>
}
</tbody>
</table>